#ifndef TRANSFORMERCONTENTPORTSTREAM_HPP_3
#define TRANSFORMERCONTENTPORTSTREAM_HPP_3

#include "logger.hpp"
#include "transContentDefine.h"
#include "transContentErrorCode.h"

class TransContentPortStream{
	private:
		int localPort;
		int remotePort;
		NodeAddr target;
		int acceptPort;
		int canBeRemoved;
		int timeout;
		pthread_mutex_t waitMutex;
		pthread_cond_t waitCond;
		TransStreamInMemory* streamIn;
		TransStreamOutMemory* streamOut;
	public:
		TransContentPortStream(int port, ConnectPeer* mPeer): localPort(port), remotePort(mPeer->remotePort), target(mPeer->remoteAddr), acceptPort(mPeer->acceptPort), canBeRemoved(0), timeout(connect_streamPortwait_time), waitMutex(PTHREAD_MUTEX_INITIALIZER), waitCond(PTHREAD_COND_INITIALIZER){
			debug_enter();
			streamIn = new TransStreamInMemory();
			streamOut = new TransStreamOutMemory();
		}
		~TransContentPortStream(){
			debug_enter();
			if(streamIn != NULL){
				delete streamIn;
				streamIn = NULL;
			}

			if(streamOut != NULL){
				delete streamOut;
				streamOut = NULL;
			}
			debug_leave();
		}

		int getLocalPort(){
			return this->localPort;
		}
		//	void setLocalPort(int port);

		int getRemotePort(){
			return this->remotePort;
		}
		//	void setRemotePort(int port);

		NodeAddr getRemoteAddr(){
			return this->target;
		}
		//	void setRemoteAddr(const NodeAddr* node);

		int getAcceptPort(){
			return this->acceptPort;
		}
		//	void setAcceptPort(int port);

		void setReceiveCallback(recvSyncCallback rscb){
			streamIn->setReceiveCallback(rscb);
		}
		recvSyncCallback getReceiveCallback(){
			return streamIn->getReceiveCallback();
		}

		void setSendCallback(sendSyncCallback sscb){
			streamOut->setSendCallback(sscb);
		}
		sendSyncCallback getSendCallback(){
			return streamOut->getSendCallback;
		}

		int SendData(const char* data, int dataLen){
			return streamOut->SendData(data, dataLen);
		}

		int getData(char* buffer, int bufferLen){
			return streamIn->getData(buffer, bufferLen);
		}

		int getBufferLeftBytes(){
			return streamOut->getBufferLeftBytes();
		}

		void setRemoteStreamStart(int startId){
			streamIn->setRemoteStreamStart(startId);
		}

		void setRemoteBufferSize(int size){
			streamOut->setRemoteBufferSize(size);
		}

		int receive(int flag, void* buffer, int bufferLen, int timeout, recvSyncCallback rscb){
			return streamIn->receive(flag, buffer, bufferLen, timeout, rscb);
		}

		int send(int flag, const void* buffer, int bufferLen, int timeout, sendSyncCallback sscb){
			return streamOut->send(flag, buffer, bufferLen, timeout, sscb);
		}

		int response(char* content, int index);

		int inti_sync();

		///
		/// each 1 second do something
		///
		void onTimer();

	private:
		///
		/// data come in response
		///
		void dataInResponse(char* content);

		///
		/// data ack response
		///
		void dataAckResponse(char* content);

		///
		/// port close response
		///
		void portCloseResponse(char* content);

		///
		/// port heart beat response
		///
		void heartBeatResponse(char* content);

		///
		/// stream start response
		///
		void streamStartResponse(char* content);
};
#endif //TRANSFORMERCONTENTPORTSTREAM_HPP_3
